﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    public class TableInfo
    {
        public string ColumnSourceID { get; set; }
        public string SourceSysName { get; set; }
        private string _ViewName = string.Empty;
        public string ViewName { get; set; }
        public string DisplayName { get; set; }
        public string GUID { get; set; }
        public string TableName { get; set; }

        private string _columnName = string.Empty;
        public string ColumnName
        {
            get
            {
                if (!string.IsNullOrEmpty(_columnName))
                {
                    string specialStr = "|!@#$%^&*|";
                    if (_columnName.Contains(specialStr))
                    {
                        int specialStrIndex = _columnName.IndexOf(specialStr);
                        return _columnName.Substring(specialStrIndex + specialStr.Length);
                    }
                    return _columnName;
                }
                else
                {
                    return _columnName;
                }
            }
            set
            {
                _columnName = value;
            }
        }

        private string _columnType = string.Empty;
        public string ColumnType
        {
            get
            {
                return FomatColumnType(_columnType.ToLower(), this.Length.ToString(), this.Precision.ToString(), this.DecimalPlaces.ToString());
            }
            set
            {
                _columnType = value;
            }
        }

        public string ColumnTypeSource { get; set; }

        public string FactColumnName1 { get; set; }

        private string _factColumnName2 = "--请选择--";
        public string FactColumnName2
        {
            get
            {
                return _factColumnName2;
            }
            set
            {
                _factColumnName2 = value;
            }
        }

        private string _factColumnName3 = "--请选择--";
        public string FactColumnName3
        {
            get
            {
                return _factColumnName3;
            }
            set
            {
                _factColumnName3 = value;
            }
        }

        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// 精度
        /// </summary>
        public int Precision { get; set; }

        /// <summary>
        /// 小数位
        /// </summary>
        public int DecimalPlaces { get; set; }

        public string DefaultValue { get; set; }
        public string FixValue { get; set; }
        public string Expression { get; set; }
        public string ScriptExtend { get; set; }

        private string _pk = string.Empty;
        public string PK
        {
            get
            {
                return _pk == "1" || _pk == "是" ? "是" : "否";
            }
            set
            {
                _pk = value;
            }
        }

        private string _isNull = string.Empty;
        public string IsNull
        {
            get
            {
                if (!string.IsNullOrEmpty(_isNull))
                {
                    return _isNull == "是" || _isNull == "1" ? "1" : "0";
                }
                return _isNull;
            }
            set
            {
                _isNull = value;
            }
        }

        public string Desc { get; set; }
        public int Count { get; set; }
        private string FomatColumnType(string columnType, string columnLength, string columnPrecision, string decimalPlaces)
        {
            string result = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(columnType))
                {
                    if (columnType.Equals("binary") || columnType.Equals("char") || columnType.Equals("datetime2") || columnType.Equals("datetimeoffset") || columnType.Equals("nchar")
                        || columnType.Equals("time") || columnType.Equals("varbinary") || columnType.Equals("varchar"))
                    {
                        result = string.Format("{0}({1})", columnType, columnLength);
                    }
                    else if (columnType.Equals("nvarchar"))
                    {
                        if (columnLength.Equals("-1"))
                        {
                            result = "nvarchar(max)";
                        }
                        else
                        {
                            result = string.Format("{0}({1})", columnType, columnLength);
                        }
                    }
                    else if (columnType.Equals("uniqueidentifier"))
                    {
                        result = "nvarchar(max)";
                    }
                    else if (columnType.Equals("numeric") || columnType.Equals("decimal"))
                    {
                        result = string.Format("{0}({1},{2})", columnType, columnPrecision, decimalPlaces);
                    }
                    else
                    {
                        result = columnType;
                    }
                }
                else
                {
                    result = "";
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
